import Vue from 'vue'
import Router from 'vue-router'
import myindex from '@/components/myindex'
import myreg from '@/components/myreg'
import mylogin from '@/components/mylogin'
import myhome from '@/components/myhome'
import showcate from '@/components/showcate'
import addcourse from '@/components/addcourse'
import courselist from '@/components/courselist'
import coursedetail from '@/components/coursedetail'

import ant_reg from '@/components/ant_reg'
import ant_login from '@/components/ant_login'
import myback from '@/components/myback'

import axios from 'axios'

// 用户管理
import user from '@/components/myback_UserControl/user'
import add_user from '@/components/myback_UserControl/add_user'
import del_user from '@/components/myback_UserControl/del_user'
import update_user from '@/components/myback_UserControl/update_user'
import find_user from '@/components/myback_UserControl/find_user'

// 用户详情
import detail from '@/components/myback_UserDetails/detail'
import detail_update_user from '@/components/myback_UserDetails/detail_update_user'
import {message} from "ant-design-vue";

// 权限管理
import authority from '@/components/myback_SuperSUPro/authority'

// 工单管理
import order_ledger from '@/components/myback_WorkOrder/order_ledger'
import reply from '@/components/myback_WorkOrder/Reply'
import leave from '@/components/myback_WorkOrder/Leave'
import CreateWork from '@/components/myback_WorkOrder/CreateWork'
import work_detail from '@/components/myback_WorkOrder/work_detail'
import upload from '@/components/myback_WorkOrder/upload'
import audit_workorder from '@/components/myback_WorkOrder/audit_workorder'
import message_work from '@/components/myback_WorkOrder/message_work'
import message_show from '@/components/myback_WorkOrder/message_show'

//websocket 
import ant_socket from '@/components/myback_WorkOrder/ant_socket'
import socket from '@/components/myback_WorkOrder/socket'

// 三方登录
import threelogin from '@/components/ThreePartiesLogin'
import message_xiang from '@/components/myback_WorkOrder/message_xiang'
import chatroom from '@/components/chatroom'
Vue.use(Router)

var routes = [
  {
    path: '/chatroom',
    name: 'chatroom',
    component: chatroom
  },
  {
    path: '/',
    name: 'ant_login',
    component: ant_login
  },
  
  {
    path: '/myindex',
    name: 'myindex',
    component: myindex
  },
  {
    path: '/myreg',
    name: 'myreg',
    component: myreg
  },
  {
    path: '/ant_reg',
    name: 'ant_reg',
    component: ant_reg
  },
  {
    path: '/threelogin',
    name: 'threelogin',
    component: threelogin
  },
  {
    path: '/myback',
    name: 'myback',
    component: myback,
    children:[
      // 用户管理
      {
        path: '/user',
        name: 'user',
        component: user,
        // 路由守卫
        beforeEnter:(to,from,next)=>{
          var uid=localStorage.getItem('uid')
            axios.get('http://127.0.0.1:8000/z/middle/?uid='+uid)
              .then(res=>{
                var level=res.data.middle
                if(level){
                  next()
                }
                else{
                  message.info('您无权限访问！')
                }
              })
        },
        children:[
          // 增
            {
              path: '/user/add_user',
              name: 'add_user',
              component: add_user,
            },
          // 删
            {
              path: '/user/del_user',
              name: 'del_user',
              component: del_user,
            },
          // 改
            {
              path: '/user/update_user',
              name: 'update_user',
              component: update_user,
            },
          // 查
            {
              path: '/user/find_user',
              name: 'find_user',
              component: find_user,
            },
        ]
      },
      // 用户详情
      {
        path: '/detail',
        name: 'detail',
        component: detail,
        beforeEnter:(to,from,next)=> {
          var uid = localStorage.getItem('uid')
          axios.get('http://127.0.0.1:8000/z/middle/?uid=' + uid)
            .then(res => {
              var level = res.data.middle
              if (!level) {
                next()
              } else {
                message.info('您无权限访问！')
              }
            })
        },
        children:[
          // 改用户名
          {
            path: '/detail/detail_update_user',
            name: 'detail_update_user',
            component: detail_update_user,
          },
          ]
      },
      // 权限管理
      {
        path: '/authority',
        name: 'authority',
        component: authority
      },
      // 工单管理
      {
        path: '/CreateWork',
        name: 'CreateWork',
        component: CreateWork,
        children: [
          {
            path: '/order_ledger',
            name: 'order_ledger',
            component: order_ledger
          },
          {
            path: '/reply',
            name: 'reply',
            component: reply
          },

        ]
      },
      // 审核工单
      {
        path: '/audit_workorder',
        name: 'audit_workorder',
        component: audit_workorder
      },
      {
        path: '/work_detail',
        name: 'work_detail',
        component: work_detail
      },
      // 消息盒子
      {
        path: '/message_work',
        name: 'message_work',
        component: message_work
      },
      {
        path: '/message_show',
        name: 'message_show',
        component: message_show
      },
      
      {
        path: '/socket',
        name: 'socket',
        component: socket
      },
    ],
  },
  // 工单
  {
    path: '/leave',
    name: 'leave',
    component: leave
  },
  {
    path: '/upload',
    name: 'upload',
    component: upload
  },

  {
    path: '/courselist',
    name: 'courselist',
    component: courselist
  },
  {
    path: '/coursedetail',
    name: 'coursedetail',
    component: coursedetail
  },
  {
    path: '/addcourse',
    name: 'addcourse',
    component: addcourse
  },
  {
    path: '/mylogin',
    name: 'mylogin',
    component: mylogin
  },
  {
    path: '/showcate',
    name: 'showcate',
    component: showcate
  },
  {
    path: '/xiangqinga',
    name: 'message_xiang',
    component: message_xiang
  },
  {
    path: '/ant_socket',
    name: 'ant_socket',
    component: ant_socket
  },
  {
    path: '/myhome',
    name: 'myhome',
    component: myhome,
    //拦截器使用
    beforeEnter: (to, from, next) => {

      if (localStorage.getItem("username")) {
        console.log('登录了');
        next();
      } else {
        next('/mylogin');
      }

    }
  }
  
]

export default new Router({
  routes: routes,
  mode: 'history'   /*hash*/
})
